Themes

There are a lot of built-in themes in ggplot and you can use them in two ways, by stating before your plot to set the theme:

theme_set(theme_bw())

or by adding them to your plot directly:

my_plot + theme_bw()

There is also a great library called ggthemes which adds even more built-in themes for ggplot. You can also customize your own themes, we'll have resources for you to do this at the end of the lecture.

Built-in Themes

Let's show some simple examples

In [3]:
library(ggplot2)
In [4]:
df <- mtcars
In [5]:
head(df)
Out[5]:
mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Datsun 71022.84108933.852.3218.611141
Hornet 4 Drive21.462581103.083.21519.441031
Hornet Sportabout18.783601753.153.4417.020032
Valiant18.162251052.763.4620.221031
In [10]:
pl <- ggplot(df,aes(x=mpg,y=hp)) + geom_point()
In [12]:
print(pl)
In [14]:
pl + theme_bw()
In [15]:
pl + theme_classic()
In [16]:
pl + theme_dark()
In [17]:
pl + theme_get()
In [19]:
pl + theme_light()
In [21]:
pl + theme_linedraw()
In [22]:
pl + theme_minimal()
In [25]:
pl + theme_void()

ggthemes

Here's a link to the documentation for ggthemes. Let's see just a few examples:

In [29]:
library(ggthemes)
In [31]:
pl + theme_excel()
In [32]:
pl + theme_economist()
In [33]:
pl + theme_economist_white()

Custom Themes

If you want to make your own custom themes (although I would highly suggest you take the time to research for existing similar themes). YOu can create your own by following the extensive documentation